//
// Created by Administrator on 2024/3/17.
//
// O(nlog(m))二分答案
#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'
const int maxn = 1e6 + 20;
ll a[maxn], n, m;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> a[i];
    cin >> m;
    ll left = *max_element(a + 1, a + n + 1);
    ll right = accumulate(a + 1, a + n + 1, 0ll);
    ll best = right;
    while (left <= right) {
        ll mid = (left + right) / 2;
        ll cnt = 0, sum = 0;
        for (int i = 1; i <= n; i++) {
            if (a[i] + sum > mid) {
                cnt++;
                sum = a[i];
            } else {
                sum += a[i];
            }
        }
        if (sum)++cnt;
        if (cnt <= m) {
            best = min(best, mid);
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    cout << best << endl;

    return 0;
}
